		TITLE	CVSEGTBL - Copyright (c) SLR Systems 1994

		INCLUDE	MACROS
		INCLUDE	SEGMENTS
		INCLUDE	GROUPS
		INCLUDE	CLASSES
if	fg_pe
		INCLUDE	PE_STRUC
		INCLUDE	EXES
endif
		INCLUDE	CVSTUFF

		PUBLIC	CV_SEGTBL_4


		.DATA

		EXTERNDEF	CV_TEMP_RECORD:BYTE,SYMBOL_TEXT:BYTE

		EXTERNDEF	CURNMOD_NUMBER:DWORD,BYTES_SO_FAR:DWORD,PE_BASE:DWORD,CV_SEGMENT_COUNT:DWORD,GROUP_COUNT:DWORD
		EXTERNDEF	FIRST_NAMESP_GINDEX:DWORD,FIRST_CV_SEGMENT_GINDEX:DWORD,FIRST_CV_GROUP_GINDEX:DWORD
		EXTERNDEF	FIRST_PEOBJECT_GINDEX:DWORD

		EXTERNDEF	GROUP_GARRAY:STD_PTR_S,SEGMENT_GARRAY:STD_PTR_S,CLASS_GARRAY:STD_PTR_S,RELOC_STUFF:ALLOCS_STRUCT
		EXTERNDEF	RELOC_GARRAY:STD_PTR_S,PE_OBJECT_GARRAY:STD_PTR_S,PEXEHEADER:PEXE

		EXTERNDEF	CV_DWORD_ALIGN:DWORD


		.CODE	PASS2_TEXT

		EXTERNDEF	INSTALL_CV_NAMESPACE:PROC,FLUSH_CV_TEMP:PROC,HANDLE_CV_INDEX:PROC,_release_minidata:proc
		EXTERNDEF	RELEASE_GARRAY:PROC,SAY_VERBOSE:PROC


;CVSEGTBL_RECORD	RECORD	FREAD:1,FWRITE:1,FEXECUTE:1,F32BIT:1,RESVL:4,FSEL:1,FABS:1,RESV:2,FGROUP:1,RESVH:3
CVSEGTBL_RECORD	RECORD	RESVH:3,FGROUP:1,RESV:2,FFABS:1,FSEL:1,RESVL:4,F32BIT:1,FEXECUTE:1,FWRITE:1,FREAD:1


CV_SEGTBL_4	PROC
		;
		;OUTPUT TABLE OF SEGMENT DESCRIPTORS, FOLLOWED BY GROUP DESCRIPTORS
		;
		;THEN OUTPUT NAME-TABLE
		;
		MOV	EAX,OFF DOING_SSTSEGTBL_MSG
		CALL	SAY_VERBOSE

		CALL	CV_DWORD_ALIGN

		PUSHM	EDI,ESI,EBX

		MOV	EAX,BYTES_SO_FAR
		MOV	EDI,OFF CV_TEMP_RECORD

		PUSH	EAX
if	fg_pe
		GETT	AL,OUTPUT_PE

		OR	AL,AL
		JZ	L1$

		MOV	AX,PEXEHEADER._PEXE_N_OBJECTS

		MOV	[EDI],AX

		MOV	[EDI+2],AX
		ADD	EDI,4

		CALL	OUTPUT_CV_SECTIONS
		JMP	L2$

L1$:
endif

		MOV	EAX,GROUP_COUNT
		MOV	ECX,CV_SEGMENT_COUNT

		MOV	[EDI+2],ECX		;# OF SEGMENTS
		ADD	EAX,ECX

		MOV	[EDI],AX		;TOTAL # OF SEGMENTS AND GROUPS
		ADD	EDI,4

		CALL	OUTPUT_CV_SEGMENTS

		CALL	OUTPUT_CV_GROUPS
L2$:
		CALL	FLUSH_CV_TEMP

		POP	EAX
		MOV	ECX,12DH

		MOV	CURNMOD_NUMBER,-1

		CALL	HANDLE_CV_INDEX

		CALL	CV_DWORD_ALIGN
		;
		;NOW OUTPUT NAMESPACE TABLE...
		;
		MOV	EAX,BYTES_SO_FAR	;SAVE START OF TABLE
		MOV	EDI,OFF CV_TEMP_RECORD

		PUSH	EAX
		MOV	ESI,FIRST_NAMESP_GINDEX

		JMP	TEST_NAMESP

NAMESP_LOOP:
		CONVERT	ESI,ESI,RELOC_GARRAY
		ASSUME	ESI:PTR CV_NAMESP_STRUCT

		MOV	EAX,[ESI]._CNS_NEXT_GINDEX
		LEA	ESI,[ESI]._CNS_TEXT

		PUSH	EAX
L7$:
		MOV	AL,BYTE PTR[ESI]
		INC	ESI

		MOV	[EDI],AL
		INC	EDI

		OR	AL,AL
		JNZ	L7$

		CMP	EDI,OFF CV_TEMP_RECORD+CV_TEMP_SIZE-SYMBOL_TEXT_SIZE-2
		JC	L75$

		CALL	FLUSH_CV_TEMP
L75$:
		POP	ESI
TEST_NAMESP:
		TEST	ESI,ESI
		JNZ	NAMESP_LOOP

		CALL	FLUSH_CV_TEMP

		POP	EAX
		MOV	ECX,12EH

		MOV	CURNMOD_NUMBER,-1

		CALL	HANDLE_CV_INDEX

		CALL	CV_DWORD_ALIGN
		;
		;RELEASE NAMESPACE TABLE
		;
		XOR	EAX,EAX
		MOV	CURNMOD_NUMBER,EAX

		MOV	EAX,OFF RELOC_STUFF
		push	EAX
		call	_release_minidata
		add	ESP,4

		MOV	EAX,OFF RELOC_GARRAY
		CALL	RELEASE_GARRAY

		POPM	EBX,ESI,EDI

		RET

CV_SEGTBL_4	ENDP


OUTPUT_CV_SEGMENTS	PROC	NEAR
		;
		;
		;
		MOV	ESI,FIRST_CV_SEGMENT_GINDEX
		JMP	TEST_SEG

SEG_LOOP:
		CONVERT	ESI,ESI,SEGMENT_GARRAY
		ASSUME	ESI:PTR SEGMENT_STRUCT

		MOV	EAX,[ESI]._SEG_NEXT_CV_GINDEX

		PUSH	EAX
		CALL	OUTPUT_CV_SEGMENT

		POP	ESI
TEST_SEG:
		TEST	ESI,ESI
		JNZ	SEG_LOOP

		RET

OUTPUT_CV_SEGMENTS	ENDP


OUTPUT_CV_SEGMENT	PROC	NEAR
		;
		;FIRST IS FLAGS WORD
		;
		;	FREAD:1,FWRITE:1,FEXECUTE:1,F32BIT:1,RESVL:4,FSEL:1,FFABS:1,RESV:2,FGROUP:1,RESVH:3
		;
		XOR	EAX,EAX
		CALL	DO_FLAGS_OVR

		MOV	EAX,[ESI]._SEG_GROUP_GINDEX

		TEST	EAX,EAX
		JZ	L21$

		CONVERT	EAX,EAX,GROUP_GARRAY
		ASSUME	EAX:PTR GROUP_STRUCT

		MOV	EAX,[EAX]._G_NUMBER
		MOV	ECX,CV_SEGMENT_COUNT

		ADD	EAX,ECX
L21$:
		MOV	[EDI],EAX			;GGR GROUP #
		ADD	EDI,4
		;
		;FRAME
		;
		GETT	CL,OUTPUT_SEGMENTED
		MOV	EAX,[ESI]._SEG_FRAME

		OR	CL,CL
		JNZ	L29$

		SHR	EAX,4
L29$:
		MOV	[EDI-2],AX			;FRAME
		MOV	ECX,[ESI]._SEG_LEN

		MOV	EAX,[ESI]._SEG_OFFSET
		PUSH	ECX
L31$:
		GETT	CL,OUTPUT_SEGMENTED
		MOV	EDX,[ESI]._SEG_FRAME

		OR	CL,CL
		JNZ	L32$

		SUB	EAX,EDX
L32$:
		MOV	ECX,[ESI]._SEG_CLASS_GINDEX
		PUSH	EAX

		PUSH	ECX
		;
		;OK, FIRST DO SEGMENT NAME
		;
		LEA	EAX,[ESI]._SEG_TEXT
		CALL	INSTALL_CV_NAMESPACE	;RETURNS OFFSET

		MOV	[EDI],EAX
		POP	EAX

		CONVERT	EAX,EAX,CLASS_GARRAY

		ADD	EAX,CLASS_STRUCT._C_TEXT
		CALL	INSTALL_CV_NAMESPACE	;RETURNS OFFSET

		MOV	[EDI+2],AX
		POP	EAX			;PHYS_OFFSET

		POP	ECX			;LENGTH
		MOV	[EDI+4],EAX

		MOV	[EDI+8],ECX
		ADD	EDI,12

		CMP	EDI,OFF CV_TEMP_RECORD+CV_TEMP_SIZE-20
		JNC	FLUSH_CV_TEMP

		RET

OUTPUT_CV_SEGMENT	ENDP


DO_FLAGS_OVR	PROC	NEAR
		;
		;
		;
		MOV	DL,[ESI]._SEG_32FLAGS
		MOV	EBX,EAX

		MOV	AL,[ESI]._SEG_TYPE

		AND	DL,MASK SEG32_USE32
		JZ	L0$

		OR	EBX,MASK F32BIT
L0$:
		TEST	AL,MASK SEG_ASEG
		JZ	L01$

		OR	EBX,MASK FFABS
L01$:
		GETT	DL,OUTPUT_PE
		GETT	CL,OUTPUT_SEGMENTED

		OR	CL,DL
		JZ	L1$
L03$:
		MOV	EAX,[ESI]._SEG_OS2_FLAGS
		OR	EBX,MASK FSEL		;MARK THIS SELECTOR BASED
		;
		;NEED TO DETERMINE FREAD, FWRITE, FEXECUTE
		;
		TEST	AL,MASK SR_TYPE
		JZ	L04$
		;
		;DATA
		;
		OR	EBX,MASK FREAD

		TEST	AL,MASK SR_EO_RO	;READ-ONLY?
		JNZ	L19$

		OR	EBX,MASK FWRITE
		JMP	L19$

L04$:
		;
		;CODE
		;
		OR	EBX,MASK FEXECUTE

		TEST	AL,MASK SR_EO_RO
		JNZ	L19$

		OR	EBX,MASK FREAD
		JMP	L19$

L1$:
		;
		;REAL MODE, MAKE A GUESS ON FREAD, FWRITE, FEXECUTE
		;
		OR	EBX,MASK FREAD+MASK FWRITE

		TEST	AL,MASK SEG_CLASS_IS_CODE
		JZ	L11$

		OR	EBX,MASK FEXECUTE
L11$:
L19$:
		;
		;WHEW, NOW OVERLAY #
		;
if	any_overlays
		MOV	EAX,[ESI]._SEG_SECTION_GINDEX
		OR	EAX,EAX
		JZ	L29$

		CONVERT	EAX,EAX,SECTION_GARRAY
		ASSUME	EAX:PTR SECTION_STRUCT

		MOV	EAX,[EAX]._SECT_NUMBER

		DEC	EAX

		SHL	EAX,16

		OR	EBX,EAX

L29$:
endif

		MOV	[EDI],EBX
		ADD	EDI,4

		RET

DO_FLAGS_OVR	ENDP


OUTPUT_CV_GROUPS	PROC	NEAR
		;
		;
		;
		MOV	ESI,FIRST_CV_GROUP_GINDEX
		JMP	TEST_GRP

GRP_LOOP:
		CONVERT	ESI,ESI,GROUP_GARRAY
		ASSUME	ESI:PTR GROUP_STRUCT

		MOV	EAX,[ESI]._G_NEXT_CV_GINDEX

		PUSH	EAX
		CALL	OUTPUT_CV_GROUP

		POP	ESI
TEST_GRP:
		TEST	ESI,ESI
		JNZ	GRP_LOOP

		RET

OUTPUT_CV_GROUPS	ENDP


OUTPUT_CV_GROUP	PROC	NEAR
		;
		;
		;
		PUSH	ESI
		MOV	ESI,[ESI]._G_FIRST_SEG_GINDEX

		TEST	ESI,ESI
		JZ	L1$
		CONVERT	ESI,ESI,SEGMENT_GARRAY
		ASSUME	ESI:PTR SEGMENT_STRUCT

		MOV	EAX,MASK FGROUP
		CALL	DO_FLAGS_OVR		;DEFINE FLAGS BASED ON FIRST SEGMENT... SAVES CODE...

		JMP	L14$

L1$:
		MOV	[EDI],ESI
		ADD	EDI,4
L14$:
		POP	ESI
		ASSUME	ESI:PTR GROUP_STRUCT
		XOR	EAX,EAX

		MOV	[EDI],EAX		;GGR
		ADD	EDI,4
		;
		;FRAME
		;
		MOV	EAX,[ESI]._G_FRAME
		GETT	CL,OUTPUT_SEGMENTED

		OR	CL,CL
		JNZ	L29$

		SHR	EAX,4
L29$:
		MOV	[EDI-2],AX		;FRAME
		MOV	ECX,[ESI]._G_LEN

		MOV	EAX,[ESI]._G_OFFSET
		PUSH	ECX

		GETT	DL,OUTPUT_SEGMENTED
		MOV	ECX,[ESI]._G_FRAME

		OR	DL,DL
		JNZ	L32$

		SUB	EAX,ECX
L32$:
		PUSH	EAX
		;
		;OK, FIRST DO SEGMENT NAME
		;
		LEA	EAX,[ESI]._G_TEXT
		CALL	INSTALL_CV_NAMESPACE	;RETURNS OFFSET

		OR	EAX,0FFFF0000H		;CLASS NAME IS NULL
		POP	ECX			;PHYS_OFFSET

		MOV	[EDI],EAX
		POP	EAX			;LENGTH

		MOV	[EDI+4],ECX
		MOV	[EDI+8],EAX

		ADD	EDI,12

		CMP	EDI,OFF CV_TEMP_RECORD+CV_TEMP_SIZE-20
		JNC	FLUSH_CV_TEMP

		RET

OUTPUT_CV_GROUP	ENDP


if	fg_pe

OUTPUT_CV_SECTIONS	PROC	NEAR
		;
		;
		;
		MOV	ESI,FIRST_PEOBJECT_GINDEX
		JMP	L8$

L1$:
		CONVERT	ESI,ESI,PE_OBJECT_GARRAY
		ASSUME	ESI:PTR PE_IOBJECT_STRUCT

		MOV	EAX,[ESI]._PEOBJECT_NEXT_GINDEX

		PUSH	EAX
		CALL	OUTPUT_CV_SECTION

		POP	ESI
L8$:
		TEST	ESI,ESI
		JNZ	L1$

		RET

OUTPUT_CV_SECTIONS	ENDP


OUTPUT_CV_SECTION	PROC	NEAR
		;
		;FIRST IS FLAGS WORD
		;
		;	FREAD:1,FWRITE:1,FEXECUTE:1,F32BIT:1,RESVL:4,FSEL:1,FFABS:1,RESV:2,FGROUP:1,RESVH:3
		;
		MOV	EAX,[ESI]._PEOBJECT_FLAGS
		MOV	ECX,MASK F32BIT+MASK FSEL

		TEST	EAX,MASK PEH_WRITABLE
		JZ	L1$

		OR	ECX,MASK FWRITE
L1$:
		TEST	EAX,MASK PEH_READABLE
		JZ	L2$

		OR	ECX,MASK FREAD
L2$:
		TEST	EAX,MASK PEH_EXECUTABLE
		JZ	L3$

		OR	ECX,MASK FEXECUTE
L3$:
		MOV	EAX,[ESI]._PEOBJECT_NUMBER
		MOV	[EDI],ECX		;FLAGS AND OVERLAY #

		SHL	EAX,16			;GGR GROUP # == 0
		;
		;FRAME
		;
		MOV	[EDI+4],EAX		;FRAME

		MOV	EAX,[ESI]._PEOBJECT_VSIZE
		XOR	ECX,ECX

		PUSHM	EAX,ECX
		;
		;OK, FIRST DO SECTION NAME
		;
		LEA	ESI,[ESI]._PEOBJECT_NAME
		MOV	ECX,8

		LEA	EDX,OFF SYMBOL_TEXT
		ADD	EDI,ECX
L4$:
		MOV	AL,BYTE PTR[ESI]
		INC	ESI

		MOV	[EDX],AL
		INC	EDX

		OR	AL,AL
		JZ	L5$

		DEC	ECX
		JNZ	L4$

		MOV	[EDX],CL
L5$:
		MOV	EAX,OFF SYMBOL_TEXT
		CALL	INSTALL_CV_NAMESPACE	;RETURNS OFFSET

		POP	ECX			;PHYS_OFFSET
		OR	EAX,0FFFF0000H		;NO CLASS NAME

		MOV	[EDI+4],ECX
		POP	ECX			;LENGTH

		MOV	[EDI],EAX
		MOV	[EDI+8],ECX

		ADD	EDI,12

		CMP	EDI,OFF CV_TEMP_RECORD+CV_TEMP_SIZE-20
		JNC	FLUSH_CV_TEMP

		RET

OUTPUT_CV_SECTION	ENDP

endif


		.CONST

DOING_SSTSEGTBL_MSG DB	SIZEOF DOING_SSTSEGTBL_MSG-1,'Doing SSTSEGTBL',0DH,0AH


		END

